/*
 * NotificationWindow.java
 *
 * Created on 7. April 2005, 13:44
 */

/**
 * This Class provides a little borderless window wich is jumping onthe screen to notify a new message.
 * @author  ChrM
 */

package com.jtalker.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class NotificationWindow extends JFrame {
	private static final long serialVersionUID = -3013951840120865853L;

	private String memberName = "";
	public static final int TOP_LEFT = 0;
	public static final int TOP_RIGHT = 1;
	public static final int BOTTOM_LEFT = 2;
	public static final int BOTTOM_RIGHT = 3;

	public static final int WIDTH = 100;
	public static final int HEIGHT = 40;

	private AnimationThread animationThread = null;
	int myPosition = BOTTOM_RIGHT;

	private JTalkerClientFront parentClientFront = null;

	/** Creates new form NotificationWindow */
	public NotificationWindow(JTalkerClientFront parent) {
		this.parentClientFront = parent;
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void initComponents() {// GEN-BEGIN:initComponents
		jLabelText = new JLabel();
		jButtonOK = new JButton();

		setTitle("New Message");
		setFocusable(false);
		setFocusableWindowState(false);
		setAlwaysOnTop(true);
		setUndecorated(true);
		jLabelText.setBackground(new Color(255, 255, 51));
		jLabelText.setFont(new Font("Dialog", 0, 10));
		jLabelText.setForeground(new Color(0, 0, 0));
		jLabelText.setHorizontalAlignment(SwingConstants.CENTER);
		jLabelText.setText("You have a new Message!");
		jLabelText.setHorizontalTextPosition(SwingConstants.CENTER);
		jLabelText.setOpaque(true);
		getContentPane().add(jLabelText, BorderLayout.NORTH);

		jButtonOK.setText("Show");
		jButtonOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButtonOKActionPerformed(evt);
			}
		});

		this.getContentPane().add(jButtonOK, BorderLayout.CENTER);
		pack();
	}// GEN-END:initComponents

	private void jButtonOKActionPerformed(ActionEvent evt) {
		this.parentClientFront.toFront();
		if (this.memberName != null && this.memberName != "") {
			this.parentClientFront.theClient.getMemberByName(this.memberName)
					.showChatWindow(this.parentClientFront);
		}
		this.setPosition(this.myPosition);
		this.dispose();
	}

	public void setVisible(boolean _yes) {
		if (this.isVisible() == _yes)
			return;

		if (this.animationThread != null && this.animationThread.isRunning())
			return;

		if (this.animationThread != null) {
			animationThread.stopAnimation();
		}

		this.animationThread = new AnimationThread(this);
		this.animationThread.start();
		super.setVisible(_yes);
		this.setPosition(this.myPosition);
	}

	/*
	 * public void show() { //super.show(); //this.setVisible(true); }
	 */

	public void setMemberName(String _name) {
		if (_name == null) {
			this.memberName = "";
			return;
		}
		this.memberName = _name;
		this.jLabelText.setText(this.memberName);
	}

	public void setPosition(int position) {
		this.myPosition = position;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

		switch (position) {
		case NotificationWindow.TOP_LEFT:
			this.setBounds(0, 0, WIDTH, HEIGHT);
			break;
		case NotificationWindow.TOP_RIGHT:
			this.setBounds(screenSize.width - WIDTH, 0, WIDTH, HEIGHT);
			break;
		case NotificationWindow.BOTTOM_LEFT:
			this.setBounds(0, screenSize.height - HEIGHT, WIDTH, HEIGHT);
			break;
		case NotificationWindow.BOTTOM_RIGHT:
			this.setBounds(screenSize.width - WIDTH,
					screenSize.height - HEIGHT, WIDTH, HEIGHT);
			break;
		}
	}

	public void dispose() {
		if (animationThread != null){
			this.animationThread.stopAnimation();
		}
		super.dispose();
		// this.setVisible(false);
	}

	private JButton jButtonOK;
	private JLabel jLabelText;

	/** Thread for some gimmicks */
	class AnimationThread extends Thread {
		NotificationWindow parent = null;
		private static final int STEP_SIZE = 1;
		private static final int MOVE_AREA = 50;
		// private int stepX = STEP_SIZE;
		private int stepY = STEP_SIZE;
		private boolean isRunning = true;

		public AnimationThread(NotificationWindow _parent) {
			this.parent = _parent;

			if (_parent.myPosition == NotificationWindow.TOP_LEFT) {
				// stepX *= -1;
				stepY *= -1;
			}
		}

		public void stopAnimation() {
			this.isRunning = false;
		}

		public boolean isRunning() {
			return this.isRunning;
		}

		public void run() {
			int runs = MOVE_AREA;

			while (runs > 0 && this.isRunning) {
				this.parent.setLocation(
						this.parent.getLocation().x /* - stepX */,
						this.parent.getLocation().y - stepY);
				try {
					Thread.sleep(100);
				} catch (Exception _e) {
				}

				runs--;

				if (runs == (int) (MOVE_AREA / 2)) {
					// stepX *= -1;
					// stepY *= -1;
					this.isRunning = false;
					this.parent.setPosition(this.parent.myPosition);
				}
			}

			this.isRunning = false;
			this.parent.dispose();
		}
	}
}
